package com.tianqu.tms.www.proxy.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.util.car.TmsCarModelInfoApi;
import com.tianqu.tms.dto.BaseCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.www.proxy.viewhelper.CarProxyInputViewHelper;

/**
 * 
 * 进入待办受理录入结果页面Controller
 * 
 * @author Administrator
 * 
 */

public class CarProxyInputResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarProxyInputResultController.class);

	private static final String SYSPATH = "sysPath";//

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			CarProxyInputViewHelper view = (CarProxyInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			Car car = new Car();
			car = Car.getInstance().findCarBySerial(view.getBasicCarId());
			TmsUserProfile profile = uc.getUserProfile();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(view.getSerial(), "");
			// 如果页面输入的车架号和之前车架号（VIN）不一样
			if (!accept.getVin().equals(view.getVin())) {
				String modelId = TmsCarModelInfoApi.getInstance().getCarModelInfoByVin(view.getVin());
				if (!StringUtil.isNullOrBlank(modelId)) {
					CarStyle carStyle = CarStyle.getInstance();
					carStyle = carStyle.findCarStyleInfo(modelId);
					if (!StringUtil.isNullOrBlank(carStyle.getCarStyleId())) {
						accept.setIsMatch(2);
						accept.setVendorId(carStyle.getVendorId());
						accept.setCarName(carStyle.getCarStyleName());
						accept.setBrandId(carStyle.getBrandId());
						accept.setCarStyleId(carStyle.getCarStyleId());
					}
				}
			}

			// 更新过户表
			accept.setSerial(view.getSerial());
			accept.setVin(view.getVin());
			accept.setCy_cardType(view.getCardType()); // check
			accept.setCarOwner(view.getCarOwner());// check
			accept.setOwnerAddress(view.getOwnerAddress());// check
			accept.setOwnerPhone(view.getOwnerPhone());// check
			if (!StringUtil.isNullOrBlank(view.getCarOwner()) && view.getCarOwner().length() >= 5) {
				accept.setOwnerType(2);// 企业
				accept.setOwnerOrgNo(view.getIdentification());
			} else if (!StringUtil.isNullOrBlank(view.getCarOwner()) && view.getCarOwner().length() < 5) {
				accept.setOwnerType(1);// 个人
				accept.setCarOwnerIdCardNo(view.getIdentification());
			}
			accept.setWaitForStatus(13);
			accept.setDb_printNum(1);
			accept.setAttachMemberId(view.getMemberId());
			accept.setAgentName(view.getAgentName());
			accept.setAgentAddress(view.getAgentAddress());
			accept.setAgentPhone(view.getAgentPhone());
			accept.setAgentOwnerIdCardNo(view.getAgentOwnerIdCardNo());
			accept.setMaterialImages1(view.getMaterialImages1());
			accept.setMaterialImages2(view.getMaterialImages2());
			accept.setMaterialImages3(view.getMaterialImages3());
			accept.setMaterialImages4(view.getMaterialImages4());
			accept.setMaterialImages5(view.getMaterialImages5());
			accept.setOwnersGroupPhoto(view.getOwnersGroupPhoto());
			accept.setMemberId(view.getMemberId());
			accept.setUpdateUserId(profile.getUniqueId());
			accept.setCy_carType(view.getCarType());
			accept.setDb_printUserId(profile.getUniqueId());
			String sysPath = (String) uc.getAttribute(SYSPATH);
			accept.updateAcceptInfoAtProxy(accept, sysPath);

			//
			car = Car.getInstance().findCarBaseInfoByVin(view.getVin());
			BaseCarInfoDto dto = new BaseCarInfoDto();
			dto.setVin(view.getVin());
			dto.setCountryStyle(Integer.parseInt(view.getCountryStyle()));
			dto.setCarUse(Integer.parseInt(view.getCarUse()));
			if (StringUtil.isNotBlank(view.getCarOwner())) {
				if (view.getCarOwner().length() > 5) dto.setCardType(2);
				else dto.setOwnerType(1);
			}
			dto.setRegisterCardNo(view.getRegisterCardNo());
			dto.setCarOwner(view.getCarOwner());
			dto.setOwnerAddress(view.getOwnerAddress());
			dto.setOwnerPhone(view.getOwnerPhone());
			dto.setCarOwnerIdCardNo(view.getCarOwnerIdCardNo());
			dto.setOwnerOrgNo(view.getOwnerOrgNo());
			dto.setModelCode(view.getModelCode());
			dto.setVendorCode(view.getVendorCode());
			dto.setCarType(Integer.parseInt(view.getCarType()));
			TransferAcceptedInfo acceptedInfo = accept.findAcceptInfoByVin(view.getVin());
			if (acceptedInfo != null && acceptedInfo.getIsMatch()==2) {
				dto.setCarName(acceptedInfo.getCarName());
				dto.setVendorId(acceptedInfo.getVendorId());
				dto.setBrandId(acceptedInfo.getBrandId());
				dto.setCarStyleId(acceptedInfo.getCarStyleId());
			} else {
				dto.setCarName(view.getCarName());
				dto.setVendorId(view.getVendorId());
				dto.setBrandId(view.getBrandId());
				dto.setCarStyleId(view.getCarStyleId());
			}
			if (car != null) {// update
				Car.getInstance().updateBaseCarInfo(dto);
			} else {// insert
				Car.getInstance().insertBaseCarInfo(dto);
			}
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 
	 * 将viewhelp中值set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private Car setEntityFromViewhelper(Car entity, CarProxyInputViewHelper view) throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		entity.setCarName(view.getCarName());
		entity.setEngine(view.getEngine());
		entity.setLicensePlate(view.getLicensePlate());
		if (view.getColor() != null && !"".equals(view.getColor())) {
			entity.setColor(Integer.parseInt(view.getColor()));
		} else {
			entity.setColor(0);
		}
		entity.setRegDate(view.getRegDate());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());

		entity.setCarUse(Integer.parseInt(view.getCarUse()));
		entity.setCarType(Integer.parseInt(view.getCarType()));
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCardType(Integer.parseInt(view.getCardType()));
		entity.setRegisterCardNo(view.getRegisterCardNo());
		entity.setCountryStyle(Integer.parseInt(view.getCountryStyle()));
		entity.setCompanyContactPerson(view.getCompanyContactPerson());

		// 20160409
		if (!StringUtil.isNullOrBlank(view.getMemberName()) && view.getMemberName().length() >= 5) {
			entity.setOwnerType(2);// 企业
			entity.setOwnerOrgNo(view.getIdentification());
		} else if (!StringUtil.isNullOrBlank(view.getMemberName()) && view.getMemberName().length() < 5) {
			entity.setOwnerType(1);// 个人
			entity.setCarOwnerIdCardNo(view.getIdentification());
		}

		entity.setOwnerPhone(view.getOwnerPhone());
		entity.setOwnerAddress(view.getOwnerAddress());
		entity.setCarOwner(view.getCarOwner());
		entity.setBrandId(view.getBrandId());
		entity.setCarStyleId(view.getCarStyleId());
		entity.setVendorId(view.getVendorId());
		// entity.setMemberId(Integer.parseInt(view.getMemberId()));
		return entity;
	}

	private TransferPersonalCarInfo setEntityFromViewhelper(TransferPersonalCarInfo entity,
			CarProxyInputViewHelper view) throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		entity.setCarName(view.getCarName());
		entity.setEngine(view.getEngine());
		entity.setLicensePlate(view.getLicensePlate());
		if (view.getColor() != null && !"".equals(view.getColor())) {
			entity.setColor(Integer.parseInt(view.getColor()));
		} else {
			entity.setColor(0);
		}
		entity.setRegDate(view.getRegDate());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());

		entity.setCarUse(Integer.parseInt(view.getCarUse()));
		entity.setCarType(Integer.parseInt(view.getCarType()));
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCardType(Integer.parseInt(view.getCardType()));
		entity.setRegisterCardNo(view.getRegisterCardNo());
		entity.setCountryStyle(Integer.parseInt(view.getCountryStyle()));
		entity.setCompanyContactPerson(view.getCompanyContactPerson());

		// 20160409
		if (!StringUtil.isNullOrBlank(view.getCarOwner()) && view.getCarOwner().length() >= 5) {
			entity.setOwnerType(2);// 企业
			entity.setOwnerOrgNo(view.getIdentification());
		} else if (!StringUtil.isNullOrBlank(view.getCarOwner()) && view.getCarOwner().length() < 5) {
			entity.setOwnerType(1);// 个人
			entity.setCarOwnerIdCardNo(view.getIdentification());
		}

		entity.setOwnerPhone(view.getOwnerPhone());
		entity.setOwnerAddress(view.getOwnerAddress());
		entity.setCarOwner(view.getCarOwner());
		entity.setBrandId(view.getBrandId());
		entity.setCarStyleId(view.getCarStyleId());
		entity.setVendorId(view.getVendorId());

		return entity;
	}
}
